home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_195 / microemacs / src.zoo / fileio.c < prev    next >
C/C++ Source or Header  |  1989-03-23  |  5KB  |  239 lines

  1. /*  FILEIO.C:   Low level file i/o routines
  2.         MicroEMACS 3.10
  3.  
  4.  * The routines in this file read and write ASCII files from the disk. All of
  5.  * the knowledge about files are here.
  6.  */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "etype.h"
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #if    AOSVS
  15. #define    fopen    xxfopen
  16. #endif
  17.  
  18. NOSHARE FILE *ffp;        /* File pointer, all functions. */
  19. static int eofflag;        /* end-of-file flag */
  20.  
  21. /*
  22.  * Open a file for reading.
  23.  */
  24. PASCAL NEAR ffropen(fn)
  25. char    *fn;
  26. {
  27.         if ((ffp=fopen(fn, "r")) == NULL)
  28.                 return(FIOFNF);
  29.     eofflag = FALSE;
  30.         return(FIOSUC);
  31. }
  32.  
  33. /*
  34.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  35.  * (cannot create).
  36.  */
  37. #if    AOSVS == 0
  38. PASCAL NEAR ffwopen(fn)
  39. char    *fn;
  40. {
  41.         if ((ffp=fopen(fn, "w")) == NULL) {
  42.                 mlwrite(TEXT155);
  43. /*                      "Cannot open file for writing" */
  44.                 return(FIOERR);
  45.         }
  46.         return(FIOSUC);
  47. }
  48. #endif
  49.  
  50. /*
  51.  * Close a file. Should look at the status in all systems.
  52.  */
  53. PASCAL NEAR ffclose()
  54. {
  55.     /* free this since we do not need it anymore */
  56.     if (fline) {
  57.         free(fline);
  58.         fline = NULL;
  59.     }
  60.  
  61. #if    MSDOS & CTRLZ
  62.     putc(26, ffp);        /* add a ^Z at the end of the file */
  63. #endif
  64.     
  65. #if     V7 | USG | HPUX | SUN | XENIX | BSD | WMCS | (MSDOS & (LATTICE | MSC | DTL | TURBO)) | OS2 | (ST520 & MWC)
  66.         if (fclose(ffp) != FALSE) {
  67.                 mlwrite(TEXT156);
  68. /*                      "Error closing file" */
  69.                 return(FIOERR);
  70.         }
  71.         return(FIOSUC);
  72. #else
  73.         fclose(ffp);
  74.         return(FIOSUC);
  75. #endif
  76. }
  77.  
  78. /*
  79.  * Write a line to the already opened file. The "buf" points to the buffer,
  80.  * and the "nbuf" is its length, less the free newline. Return the status.
  81.  * Check only at the newline.
  82.  */
  83. PASCAL NEAR ffputline(buf, nbuf)
  84. char    buf[];
  85. {
  86.         register int    i;
  87. #if    CRYPT
  88.     char c;        /* character to translate */
  89.  
  90.     if (cryptflag) {
  91.             for (i = 0; i < nbuf; ++i) {
  92.             c = buf[i];
  93.             crypt(&c, 1);
  94.             putc(c, ffp);
  95.         }
  96.     } else
  97.             for (i = 0; i < nbuf; ++i)
  98.                     putc(buf[i], ffp);
  99. #else
  100.         for (i = 0; i < nbuf; ++i)
  101.                 putc(buf[i], ffp);
  102. #endif
  103.  
  104. #if    ST520 & ADDCR
  105.         putc('\r', ffp);
  106. #endif        
  107.         putc('\n', ffp);
  108.  
  109.         if (ferror(ffp)) {
  110.                 mlwrite(TEXT157);
  111. /*                      "Write I/O error" */
  112.                 return(FIOERR);
  113.         }
  114.  
  115.         return(FIOSUC);
  116. }
  117.  
  118. /*
  119.  * Read a line from a file, and store the bytes in the supplied buffer. The
  120.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  121.  * at the end of the file that don't have a newline present. Check for I/O
  122.  * errors too. Return status.
  123.  */
  124. PASCAL NEAR ffgetline()
  125.  
  126. {
  127.         register int c;        /* current character read */
  128.         register int i;        /* current index into fline */
  129.     register char *tmpline;    /* temp storage for expanding line */
  130.  
  131.     /* if we are at the end...return it */
  132.     if (eofflag)
  133.         return(FIOEOF);
  134.  
  135.     /* dump fline if it ended up too big */
  136.     if (flen > NSTRING && fline != NULL) {
  137.         free(fline);
  138.         fline = NULL;
  139.     }
  140.  
  141.     /* if we don't have an fline, allocate one */
  142.     if (fline == NULL)
  143.         if ((fline = malloc(flen = NSTRING)) == NULL)
  144.             return(FIOMEM);
  145.  
  146.     /* read the line in */
  147.         i = 0;
  148.         while ((c = getc(ffp)) != EOF && c != '\n') {
  149.                 fline[i++] = c;
  150.         /* if it's longer, get more room */
  151.                 if (i >= flen) {
  152.                     if ((tmpline = malloc(flen+NSTRING)) == NULL)
  153.                         return(FIOMEM);
  154.                     bytecopy(tmpline, fline, flen);
  155.                     flen += NSTRING;
  156.                     free(fline);
  157.                     fline = tmpline;
  158.                 }
  159.         }
  160.  
  161. #if    ST520
  162.     if(fline[i-1] == '\r')
  163.         i--;
  164. #endif
  165.  
  166.     /* test for any errors that may have occured */
  167.         if (c == EOF) {
  168.                 if (ferror(ffp)) {
  169.                         mlwrite(TEXT158);
  170. /*                              "File read error" */
  171.                         return(FIOERR);
  172.                 }
  173.  
  174.                 if (i != 0)
  175.             eofflag = TRUE;
  176.         else
  177.             return(FIOEOF);
  178.         }
  179.  
  180.     /* terminate and decrypt the string */
  181.         fline[i] = 0;
  182. #if    CRYPT
  183.     if (cryptflag)
  184.         crypt(fline, strlen(fline));
  185. #endif
  186.         return(FIOSUC);
  187. }
  188.  
  189. int PASCAL NEAR fexist(fname)    /* does <fname> exist on disk? */
  190.  
  191. char *fname;        /* file to check for existance */
  192.  
  193. {
  194.     FILE *fp;
  195.  
  196.     /* try to open the file for reading */
  197.     fp = fopen(fname, "r");
  198.  
  199.     /* if it fails, just return false! */
  200.     if (fp == NULL)
  201.         return(FALSE);
  202.  
  203.     /* otherwise, close it and report true */
  204.     fclose(fp);
  205.     return(TRUE);
  206. }
  207.  
  208. #if    AZTEC & MSDOS
  209. /*    a1getc:        Get an ascii char from the file input stream
  210.             but DO NOT strip the high bit
  211. */
  212.  
  213. #undef    getc
  214.  
  215. int a1getc(fp)
  216.  
  217. FILE *fp;
  218.  
  219. {
  220.     int c;        /* translated character */
  221.  
  222.     c = getc(fp);    /* get the character */
  223.  
  224.     /* if its a <LF> char, throw it out  */
  225.     while (c == 10)
  226.         c = getc(fp);
  227.  
  228.     /* if its a <RETURN> char, change it to a LF */
  229.     if (c == '\r')
  230.         c = '\n';
  231.  
  232.     /* if its a ^Z, its an EOF */
  233.     if (c == 26)
  234.         c = EOF;
  235.  
  236.     return(c);
  237. }
  238. #endif
  239.